package com.ruyuan.rapid.disruptor.quicklystart;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * 生产者
 */
public class OrderEventProducer {

    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void putData(ByteBuffer bb) {
        // 先获取下一个可用的序号
        long sequence = ringBuffer.next();
        try {
            // 通过可用的序号获取对应下标的数据OrderEvent对象
            OrderEvent event = ringBuffer.get(sequence);
            // 给event对象重新设置内容
            event.setValue(bb.getLong(0));
        } finally {
            // 最后发布序号
            ringBuffer.publish(sequence);
        }
    }
}
